package com.wanmait.baohan.controller.ReimbursementController;

import com.github.pagehelper.PageInfo;
import com.wanmait.baohan.pojo.*;
import com.wanmait.baohan.pojo.Process;
import com.wanmait.baohan.service.*;
import com.wanmait.baohan.vo.DataVo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.Date;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/reimbursement")
public class ReimbursementApplicationController {
    @Resource
    private ReimbursementService reimbursementService;
    @Resource
    private BookingCountService bookingCountService;
    @Resource
    private HotelAccountService hotelAccountService;
    @Resource
    private BookingTicketApplicationService ticketApplicationService;
    @Resource
    private DictionaryService dictionaryService;
    @Resource
    private BookingHotelService bookingHotelService;
    @Resource
    private TravelApplicationService travelApplicationService;

    /**
     * 所有人的报销通用申请列表
     *
     */

    @RequestMapping("/reimbursementApplication")
    public void reimbursementApplication(){

    }

    /**
     * 我的报销通用申请列表
     *
     */
    @RequestMapping("/reimbursementByUser")
    public void reimbursementByUser(){

    }





    /**
     * 获所有人的报销通用申请列表 分页
     * 条件查询
     * 周勇
     */
    @RequestMapping("getReimbursementApplications")
    @ResponseBody
    public DataVo<ReimbursementApplication> getAllApplications(Date beginDate, Date endDate, Integer page, Integer limit, String staffName, Integer enable){
//        System.out.println("111111111111111");
//        System.out.println("人的名字="+staffName);
//        System.out.println("enable状态="+enable);
        Staff staff=new Staff();
        staff.setName("".equals(staffName)?null:staffName);
        ReimbursementApplication reimbursementApplication=new ReimbursementApplication();
        reimbursementApplication.setEnable("".equals(enable)?null:enable);
        reimbursementApplication.setStaff(staff);
        //封装到vo
        DataVo<ReimbursementApplication> dataVo = new DataVo<>();
        PageInfo<ReimbursementApplication> reimbursementApplicationPageInfo = reimbursementService.selectReimbursementApplication(page, limit, reimbursementApplication,beginDate, endDate);
        List<ReimbursementApplication> reimbursementApplications = reimbursementApplicationPageInfo.getList();
        dataVo.setData(reimbursementApplications);
        //getTotal查出的总记录数
        dataVo.setCount(reimbursementApplicationPageInfo.getTotal());
        return dataVo;
    }

    /**
     * 获取我的报销通用申请列表 分页
     * 条件查询
     * 周勇
     */
    @RequestMapping("getMyApplications")
    @ResponseBody
    public DataVo<ReimbursementApplication> getMyApplications(Date beginDate, Date endDate,ReimbursementApplication reimbursementApplication,HttpServletRequest request,Integer page, Integer limit){
        //System.out.println("登录人的ID========="+staff.getId());
        Staff staff=(Staff) request.getSession().getAttribute("user");
        reimbursementApplication.setStaff(staff);
        //封装到vo
        DataVo<ReimbursementApplication> dataVo = new DataVo<>();
        PageInfo<ReimbursementApplication> reimbursementApplicationPageInfo = reimbursementService.selectReimbursementApplication(page, limit, reimbursementApplication,beginDate, endDate);
        List<ReimbursementApplication> reimbursementApplications = reimbursementApplicationPageInfo.getList();
        dataVo.setData(reimbursementApplications);
        //getTotal查出的总记录数
        dataVo.setCount(reimbursementApplicationPageInfo.getTotal());
        return dataVo;
    }


    /**
     * 报销申请
     */
    @RequestMapping("/administrativeReimbursementApplication")
    public String administrativeReimbursementApplication(Model model,Integer detailsId, HttpServletRequest request, Integer dictionaryId,Integer pastId){
        System.out.println("是否进入===============");
        System.out.println("报销类型id"+dictionaryId);
        System.out.println("申请表id"+detailsId);

        //正常申请
        Dictionary dictionary=dictionaryService.selectByValue(dictionaryId);
        Staff staff=(Staff) request.getSession().getAttribute("user");
        BookingHotelApplication bookingHotelApplication=new BookingHotelApplication();
        BookingTicketApplication bookingTicketApplication=new BookingTicketApplication();
        //重新申请
        if(pastId!=null){
            System.out.println("pastId========"+pastId);
            model.addAttribute("pastId",pastId);
        }
        if(dictionaryId==19){
            bookingTicketApplication.setId(detailsId);
            bookingTicketApplication.setStaff(staff);
            BookingTicketApplication details=ticketApplicationService.findByTicket(bookingTicketApplication);
            Integer bookingAccounts=bookingCountService.findAllCost(detailsId);
            if(bookingAccounts==null){
                bookingAccounts=0;
            }
            System.out.println("多少钱========"+bookingAccounts);
            model.addAttribute("price",bookingAccounts);
            model.addAttribute("details",details);

        }else if(dictionaryId==20){
            TravelApplication details=travelApplicationService.findById(detailsId);
            System.out.println("application=============="+details.getId());
            model.addAttribute("details",details);
        }else if(dictionaryId==37){
        }else if(dictionaryId==38){
            //System.out.println("*************");
            bookingHotelApplication.setId(detailsId);
            //System.out.println("2222222222222");
            bookingHotelApplication.setStaff(staff);
            //System.out.println("3333333333333333333");
            BookingHotelApplication details=bookingHotelService.findByHotel(bookingHotelApplication);
            System.out.println("id+++++++++++=="+details.getId());
            System.out.println("编号=============="+details.getNumbering());
            Integer hotelAccounts=hotelAccountService.findAllCost(detailsId);
            if(hotelAccounts==null){
                hotelAccounts=0;
            }
            System.out.println("多少钱========"+hotelAccounts);
            model.addAttribute("price",hotelAccounts);
            model.addAttribute("details",details);
        }
        model.addAttribute("dictionary",dictionary);
        return "/reimbursement/administrativeReimbursementApplication";
    }


//    @RequestMapping("/invoiceType")
//    @ResponseBody
//    public DataVo<Dictionary> invoiceType(){
//         DataVo<Dictionary> dictionaryDataVo=new DataVo<>();
//         List<Dictionary>dictionaries= dictionaryService.selectByType(11);
//         dictionaryDataVo.setData(dictionaries);
//        //System.out.println("集合长度："+dictionaries.size());
//         return dictionaryDataVo;
//    }
    //票类信息
    @RequestMapping("/verifyClassification")
    @ResponseBody
    public List verifyClassification(Integer typeid, HttpServletRequest request){
       // System.out.println("进入");
        List ticket=null;
      Staff staff=(Staff) request.getSession().getAttribute("user");
      Integer user=staff.getId();
        //System.out.println("id是："+user);
          switch (typeid){
              case 19://订票
                 // System.out.println("case id:"+user);
                  ticket=bookingCountService.findAllByIdTicket(user,2);//订票报销
                  break;
                 // System.out.println("长度："+ticket.size());
              case 20:

                  break;
              case 37:
                  break;
              case 38://酒店
                  ticket=hotelAccountService.findByIdHotel(user,2);//酒店报销
                  break;
          }
        return ticket;
    }
    //申请报销表单提交
    @RequestMapping("/submitReimbursement")
    public String submitReimbursement(ReimbursementApplication reimbursementApplication,Integer userId,Integer dictionaryId,Integer pastId){
//        System.out.println("开始提交");
//        System.out.println("dictionary========"+dictionaryId);
//        System.out.println("报销申请id+++++++++"+reimbursementApplication.getTravelApplicationId());
        if(pastId!=null){
            reimbursementService.updateReimbursementService(pastId,3);
        }
        Staff staff=new Staff();
        staff.setId(userId);
        Dictionary dictionary=new Dictionary();
        dictionary.setId(dictionaryId);
        //System.out.println(staff.getId());
        reimbursementApplication.setStaff(staff);
        reimbursementApplication.setDictionary(dictionary);
        reimbursementService.addReimbursementService(reimbursementApplication);

       //System.out.println("结束提交");
        return "reimbursement/reimbursementByUser";
    }


}
